/*****************************************************
** Author: 
** Date: 2019-11-14
*
*****************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include "syscom.h"
#include "comm.h"


struct syscom_node
{
    short syscomid;
    pthread_t task;
    // timer_t timer;
    char name[64];
};

#define SYSCOM_NUM      2
struct syscom_node node[SYSCOM_NUM];

syscom_handle_t *sc;
unsigned char bipbuff[2 * 1024];

/**
 * @brief
 * @param  None
 * @retval None
 */
static void syscom_handler(unsigned char *pbuf, unsigned int len)
{
    short syscomid = *(short *)pbuf;
    tc_log("recv syscomid %d", syscomid);
}

static void *syscom_task(void *arg)
{
    struct syscom_node *node = (struct syscom_node *)arg;
    int ret, time, cnt = 100;

    tc_log("%s syscom id %d producer start", node->name, node->syscomid);

    while (cnt--) {
        time = rand() % 100;
        tc_log("%s sleep %dms", node->name, time);
        usleep(time * 1000);

        ret = syscom_send(sc, node->syscomid, (unsigned char *)&node->syscomid, sizeof(short));
        tc_log("%s syscom %d send ret %d", node->name, node->syscomid, ret);
    }

    return NULL;
}

// static void syscom_timer(union sigval val)
// {
//     tc_log("sival_int %d sinval 0x%x", val.sival_int, val.sival_ptr);
//     struct syscom_node *node = (struct syscom_node *)val.sival_ptr;

//     int ret, time, cnt = 2;

//     tc_log("%s syscom id %d producer start", node->name, node->syscomid);

//     while (cnt--) {
//         time = rand() % 100;
//         tc_log("%s sleep %dms", node->name, time);
//         usleep(time * 1000);

//         ret = syscom_send(sc, node->syscomid, &node->syscomid, sizeof(short));
//         tc_log("%s syscom %d send ret %d", node->name, node->syscomid, ret);
//     }
// }

void syscom_run(void)
{
    int i, ret;

    syscom_init();

    sc = syscom_open(4 * 1024, bipbuff, sizeof(bipbuff));
    if (sc == NULL) {
        tc_log_err("open syscom failed");
        return ;
    }

    for (i = 0; i < SYSCOM_NUM; ++i) {
        node[i].syscomid = syscom_register(sc, syscom_handler);
        if (node[i].syscomid < 0) {
            tc_log_err("register syscomid failed %d", node[i].syscomid);
            continue ;
        }
        tc_log("register syscom id %d", node[i].syscomid);

        snprintf(node[i].name, sizeof(node[i].name), "thread %d", i);
        if ((ret = pthread_create(&node[i].task, NULL, syscom_task, &node[i])) != 0) {
            tc_log_err("create thread failed");
            continue ;
        }

        // snprintf(node[i].name, sizeof(node[i].name), "timer %d", i);

        // struct sigevent se;
        // se.sigev_notify = SIGEV_THREAD;
        // se.sigev_signo = SIGALRM;
        // se.sigev_value.sival_int = 77;
        // se.sigev_value.sival_ptr = (int)&node[i];
        // tc_log("node[%d] %p sigev_value 0x%x", i, &node[i], se.sigev_value);
        // se.sigev_notify_function = syscom_timer;
        // se.sigev_notify_attributes = NULL;
        // ret = timer_create(CLOCK_REALTIME, &se, &node[i].timer);
        // tc_log("create timer ret %d", ret);

        // struct itimerspec it;
        // it.it_interval.tv_sec = 1;
        // it.it_interval.tv_nsec = 0;
        // it.it_value.tv_sec = 1;
        // it.it_value.tv_nsec = 0;
        // ret = timer_settime(node[i].timer, 0, &it, NULL);
        // tc_log("set timer ret %d", ret);
    }

    for (i = 0; i < SYSCOM_NUM; ++i) {
        pthread_join(node[i].task, NULL);
    }
    // sleep(30);

    syscom_close(sc);

    tc_log("exit");
}

/* End of file */



